const app = getApp(), fetchApi = require("../../utils/httpPost.js")
import Toast from '../../component/vant/toast/toast';
import Dialog from '../../component/vant/dialog/dialog';
const CryptoJS = require("../../utils/aesUtil.js")
const message = '是否要解绑该企业？';
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    is_mer: 0,
    is_admin: 0,
    shop_name:'去绑定企业',
    orderItemArr: [],
    total_integral:0,
    isLogin:0,
    user_type:-1,
    showDialog:false,
    showPhoneDialog:false,
    dialogTitle:'绑定企业',
    dialogPhoneTitle:'绑定手机号',
    shop_no:'',
    shop_id:0,
    phone_num:'去绑定'
  },
  //事件处理函数
  bindViewTap: function() {
    // wx.navigateTo({ url: '../logs/logs' })
  },
  onLoad: function () {
  },
  openDialog:function(){
    if(this.data.shop_id > 0){//解绑
      Dialog.confirm({
        title: '解绑企业',
        message,
      });
    }else{//绑定
      // this.setData({showDialog:true})
      this.scanCode();
    }
  },
  openPhoneDialog:function(){
    this.setData({showPhoneDialog:true})
  },
  onClose:function(event){
    var that = this
    console.log('event',event)
    var btn = event.detail
    console.log('onDialogClose btn',btn)
    if(btn == 'confirm'){
      fetchApi.httpPost('/user/unBandShop', {
        shop_id: that.data.shop_id,
      }, function (res) {
        console.log('res', res)
        if(res.data.code == 0){
          Toast.success(res.data.msg)
          that.setData({shop_name:'未绑定企业'})
          that.data.shop_id = 0;
        }else{
          Toast.fail(res.data.msg)
        }
      }, function () {
        
       })
    }
  },
  shopNoInput:function(t){
    console.log('shopNoInput',t.detail.toUpperCase())
    this.setData({ shop_no: t.detail.toUpperCase() });
    this.data.shop_no = t.detail.toUpperCase()
  },
  phoneInput:function(t){
    console.log('shopNoInput',t.detail.toUpperCase())
    this.setData({ shop_no: t.detail.toUpperCase() });
    this.data.phone_num = t.detail.toUpperCase()
  },
  onDialogClose:function(event){
    var that = this
    if (that.data.shop_no == null || that.data.shop_no == '') {
      Toast.fail('请输入商户编码'); return;
    }
    console.log('onDialogClose',event)
    var btn = event.detail
    console.log('onDialogClose btn',btn)
    if(btn == 'confirm'){
      fetchApi.httpPost('/user/bandShop', {
        shop_no: that.data.shop_no,
      }, function (res) {
        console.log('res', res)
        if(res.data.code == 0){
          Toast.success(res.data.msg)
          that.data.shop_id = res.data.data.id
          that.data.shop_no = res.data.data.shopNo
          that.data.shop_name = res.data.data.shopName
          that.setData({shop_name: res.data.data.shopName})
        }else{
          Toast.fail(res.data.msg)
        }
      }, function () {
        
       })
    }
  },
  onDialogPhoneClose:function(event){
    var that = this
    if (that.data.phone_num == null || that.data.phone_num == '') {
      Toast.fail('请输入手机号'); return;
    }
    console.log('onDialogPhoneClose',event)
    var btn = event.detail
    console.log('onDialogPhoneClose btn',btn)
    if(btn == 'confirm'){
      fetchApi.httpPost('/user/bandPhone', {
        phone_num: that.data.phone_num,
      }, function (res) {
        console.log('res', res)
        if(res.data.code == 0){
          Toast.success(res.data.msg)
          that.setData({phone_num: that.data.phone_num})
        }else{
          Toast.fail(res.data.msg)
        }
      }, function () {
        
       })
    }
  },
  goLogin:function(){
    wx.navigateTo({
      url: '../login/index',
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/user/getUserInfoData', {
    }, function (res) {
      console.log("getUserInfoData 数据", res)
      if (res.data.code == 0) {
        app.setStorageSync("user_type", res.data.data.user_type);
        app.setStorageSync("shop_id", res.data.data.shop_id);
        app.setStorageSync("shop_name", res.data.data.shop_name);
        app.setStorageSync("phone_num", res.data.data.phone_num);
        that.setData({
          orderItemArr: res.data.data.orderItemArr,
          shop_name: res.data.data.shop_name,
          phone_num:res.data.data.phone_num
        })
      }
    },function(){})
  },
  onShow: function () {
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    var is_mer = app.getStorageSync("is_mer");
    var is_admin = app.getStorageSync("is_admin");
    var shop_name = app.getStorageSync("shop_name");
    var user_type = app.getStorageSync("user_type");
    var shop_id = app.getStorageSync("shop_id");
    var phone_num = app.getStorageSync("phone_num");
    this.data.shop_id = shop_id
    that.setData({
      isLogin: isLogin,
      is_mer: is_mer,
      is_admin: is_admin,
      shop_name:shop_name,
      user_type:user_type,
      phone_num:phone_num
    })
    app.globalData.userInfo = app.getStorageSync("userInfo");
    console.log('isLogin', isLogin)
    console.log('app.globalData.userInfo', app.globalData.userInfo)
    if (isLogin) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
      that.onHttpInit()
    }
  },
  scanCode: function () {
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '../login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {}
          if (qrcontent.indexOf('qrtype') < 0) {//加密过的数据
            qrcontent = CryptoJS.Decrypt(res.result)//解密
            console.log('解密：qrcontent',qrcontent)
            json_param = JSON.parse(qrcontent);
          }
          var isshop_indexof = qrcontent.indexOf('shop');
          var that = this
          if (isshop_indexof >= 0) {
            fetchApi.httpPost('/user/bandShop', {
              shop_no: json_param["num"],
            }, function (res) {
              console.log('res', res)
              if(res.data.code == 0){
                Toast.success(res.data.msg)
                that.data.shop_id = res.data.data.id
                that.data.shop_no = res.data.data.shopNo
                that.data.shop_name = res.data.data.shopName
                that.setData({shop_name: res.data.data.shopName})
              }else{
                Toast.fail(res.data.msg)
              }
            }, function () {
              
             })
          }else{
            Toast.fail('请使用巡检地点的二维码')
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
})
